﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;

namespace AttentenceCheckSystem
{
    /// <summary>
    /// CalenderWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CalenderWindow : Window
    {
        public CalenderWindow()
        {
            InitializeComponent();
        }

        public DateTime Date
        {
            set
            {
                calender.Date = value;
                Title = string.Format("节假日设置{0}年{1}月", value.Year, value.Month);
                string strConn = Properties.Settings.Default.Database1ConnectionString;
                try
                {
                    using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))
                    {
                        conn.Open();

                        System.Data.OleDb.OleDbCommand command = conn.CreateCommand();
                        command.CommandText = "SELECT Date FROM daysinfo WHERE Date BETWEEN @start AND @end";
                        DateTime startDate = common.GetCheckStartDate(value);
                        command.Parameters.Add("@start", OleDbType.Date).Value = startDate;
                        DateTime endDate = common.GetCheckEndDate(value);
                        command.Parameters.Add("@end", OleDbType.Date).Value = endDate;
                        OleDbDataReader reader = command.ExecuteReader();
                        if (reader.HasRows)
                        {
                            calender.ClearHolidays();
                            while(reader.Read())
                            {
                                calender.SetHoliday(Convert.ToDateTime(reader["Date"]), true);
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            get
            {
                return calender.Date;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            string strConn = Properties.Settings.Default.Database1ConnectionString;
            try
            {
                using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))
                {
                    conn.Open();

                    System.Data.OleDb.OleDbCommand command = conn.CreateCommand();
                    command.CommandText = "DELETE FROM daysinfo WHERE Date BETWEEN @start AND @end";
                    DateTime startDate = common.GetCheckStartDate(Date);
                    command.Parameters.Add("@start", OleDbType.Date).Value = startDate;
                    DateTime endDate = common.GetCheckEndDate(Date);
                    command.Parameters.Add("@end", OleDbType.Date).Value = endDate;
                    command.ExecuteNonQuery();

                    List<DateTime> holidays = calender.Holidays();
                    foreach (DateTime dt in holidays)
                    {
                        command.Parameters.Clear();
                        command.CommandText = "INSERT INTO daysinfo VALUES(@value)";
                        command.Parameters.Add("@value", dt);
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            DialogResult = true;
            Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}
